using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling;

namespace Evaluant.EussDSL
{
    public partial class ReferenceConnector
    {
        protected override void InitializeResources(StyleSet classStyleSet)
        {
            base.InitializeResources(classStyleSet); // don't forget!
            ReferenceConnector.AssociateValueWith(this.Store, EntityReferencesTargets.parentFieldDomainPropertyId);
        }

        protected override void OnAssociatedPropertyChanged(PropertyChangedEventArgs e)
        {
            // Called for a change in any property, so we must discriminate:

            if ("parentField".Equals(e.PropertyName))
            {
                bool is11 = true;

                //ce qu'il faut faire (en testant, etc):
                if (this.FromShape != null && this.FromShape.ModelElement != null && this.FromShape.ModelElement is Entity)
                {
                    Entity source = ((Entity)(this.FromShape.ModelElement));

                    if (source.Identifiers != null && source.Identifiers.Count>0)
                    {
                        // c'est une relation 1-* ou *-* si un des Identifiers de l'entite source est le parentField de la relation courante
                        foreach (Identifier identifier in source.Identifiers)
                        {
                            if (identifier.field == e.NewValue.ToString())
                            {
                                is11 = false;
                            }
                        }
                    }

                }

                if (is11)
                {
                    this.DecoratorTo = LinkDecorator.DecoratorEmptyArrow;
                    this.DecoratorFrom = LinkDecorator.DecoratorEmptyArrow;

                    Transaction t = Store.TransactionManager.BeginTransaction();
                    SourceMultiplicity = "1";
                    TargetMultiplicity = "1";
                    t.Commit();
                }
                else
                {
                    this.DecoratorTo = LinkDecorator.DecoratorEmptyArrow;
                    this.DecoratorFrom = null;

                    Transaction t = Store.TransactionManager.BeginTransaction();
                    SourceMultiplicity = "1";
                    TargetMultiplicity = "*";
                    t.Commit();
                }
            }


            base.OnAssociatedPropertyChanged(e);
        }
    }
}
